package com.uduemc.biso.master.backend.validator.impl;

import java.io.IOException;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.CollectionUtils;

import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.master.backend.service.ServermanageService;
import com.uduemc.biso.master.backend.validator.HostServerIdExist;
import com.uduemc.biso.master.config.SpringContextUtils;

public class HostServerIdExistValid implements ConstraintValidator<HostServerIdExist, Long> {

	@Override
	public void initialize(HostServerIdExist constraintAnnotation) {

	}

	@Override
	public boolean isValid(Long value, ConstraintValidatorContext context) {
		if (value == null) {
			return false;
		}
		ServermanageService servermanageServiceImpl = SpringContextUtils.getBean("servermanageServiceImpl",
				ServermanageService.class);

		List<SysServer> listSysServer = null;
		try {
			listSysServer = servermanageServiceImpl.getServerByTypeStatus(0, (short) 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!CollectionUtils.isEmpty(listSysServer)) {
			for (SysServer server : listSysServer) {
				if (server != null && server.getId() != null && server.getId().longValue() == value.longValue()) {
					return true;
				}
			}
		}
		return false;
	}

}
